package xyz.yuzegod.megawalls.database;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.Plugin;
import xyz.yuzegod.megawalls.MegaWalls;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Logger;

public class SQLiteCore extends DataBaseCore
{
    private static String driverName;
    private static Logger logger;
    private Connection connection;
    private File dbFile;
    
    public SQLiteCore(final File dbFile) {
        this.dbFile = dbFile;
        if (this.dbFile.exists()) {
            try {
                this.dbFile.createNewFile();
            }
            catch (IOException e) {
                SQLiteCore.logger.warning("数据库文件 " + dbFile.getAbsolutePath() + " 创建失败!");
                e.printStackTrace();
            }
        }
        try {
            Class.forName(SQLiteCore.driverName).newInstance();
        }
        catch (Exception e2) {
            SQLiteCore.logger.warning("数据库初始化失败 请检查驱动 " + SQLiteCore.driverName + " 是否存在!");
            e2.printStackTrace();
        }
    }
    
    public SQLiteCore(final Plugin plugin, final ConfigurationSection cfg) {
        this(plugin, cfg.getString("database"));
    }
    
    public SQLiteCore(final Plugin plugin, final String filename) {
        this.dbFile = new File(plugin.getDataFolder(), filename + ".db");
        if (this.dbFile.exists()) {
            try {
                this.dbFile.createNewFile();
            }
            catch (IOException e) {
                SQLiteCore.logger.warning("数据库文件 " + this.dbFile.getAbsolutePath() + " 创建失败!");
                e.printStackTrace();
            }
        }
        try {
            Class.forName(SQLiteCore.driverName).newInstance();
        }
        catch (Exception e2) {
            SQLiteCore.logger.warning("数据库初始化失败 请检查驱动 " + SQLiteCore.driverName + " 是否存在!");
            e2.printStackTrace();
        }
    }
    
    public SQLiteCore(final String filepath) {
        this(new File(filepath));
    }
    
    @Override
    public boolean createTables(final String tableName, final KeyValue fields, final String Conditions) throws SQLException {
        final String sql = "CREATE TABLE IF NOT EXISTS `%s` ( %s )";
        return this.execute(String.format(sql, tableName, fields.toCreateString().replace("AUTO_INCREMENT", "AUTOINCREMENT")));
    }
    
    public String getAUTO_INCREMENT() {
        return "AUTOINCREMENT";
    }
    
    @Override
    public Connection getConnection() {
        try {
            if (this.connection != null && !this.connection.isClosed()) {
                return this.connection;
            }
            return this.connection = DriverManager.getConnection("jdbc:sqlite:" + this.dbFile);
        }
        catch (SQLException e) {
            SQLiteCore.logger.warning("数据库操作出错: " + e.getMessage());
            SQLiteCore.logger.warning("数据库文件: " + this.dbFile.getAbsolutePath());
            return null;
        }
    }
    
    static {
        SQLiteCore.driverName = "org.sqlite.JDBC";
        SQLiteCore.logger = MegaWalls.getInstance().getLogger();
    }
}
